package com.watchthelan.service.server;

import java.net.SocketException;
import java.util.List;

import com.watchthelan.domain.Server;
import com.watchthelan.repository.ServerDao;
import com.watchthelan.service.network.SocketConnection;

public class SimpleServerManager implements ServerManager {
	private static final long serialVersionUID = -1138224925779350608L;
	private ServerDao serverDao;
	
	public void setServerDao(ServerDao serverDao) {
		this.serverDao = serverDao;
	}

	public void addServer(Server server) {
		// Set server's state
		try {
			server.setStatus(server.getRealStatus());
		} catch (SocketException e) {
			server.setStatus(SocketConnection.STATE_STOP);
		}
		serverDao.addServer(server);
	}

	public void deleteServerById(Long id) {
		serverDao.deleteServerById(id);
	}

	public List<Server> getServers() {
		return serverDao.getServers();
	}

	public Server getServerById(Long id) {
		return serverDao.getServerById(id);
	}

	public void updateServer(Server server) {
		try {
			server.setStatus(server.getRealStatus());
		} catch (SocketException e) {
			server.setStatus(SocketConnection.STATE_STOP);
		}
		serverDao.updateServer(server);
		
	}

	@Override
	public List<Server> getServersOwnedBy(Long id) {
		return serverDao.getServersOwnedBy(id);
	}
}
